/******************************************************************************
	This file is a part of the Snapshot Bioinformatics Project

	Copyright (C) 2012-2013 Jarom Schow

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include "ProgressMgr.h"

#include <QtCore/QCoreApplication>

#include "ProgressHandler.h"
#include "ProgressDlg.h"

/**
**/
class Progress : public Bio::ProgressItem {
public:
	
	Progress( const QString &msg, int totalSteps, bool canCancel, ProgressDlg *dlg );
	virtual ~Progress();

	virtual bool	isCanceled() const;

	virtual void	step( int steps );
	virtual void	update( int curSteps );
	virtual void	finish();
	virtual void	cancel();
	virtual void	status( const QString &msg );

	int			m_totalSteps, m_progressId, m_curProgress, m_buffer, m_minUpdate;
	ProgressDlg	*m_dlg;
};

/**
**/
ProgressMgr::ProgressMgr( QWidget *parent, const QString &appName ) : m_appName( appName ), m_progressDlg( NULL )
{
	m_progressDlg = new ProgressDlg( parent, m_appName );
}

/**
**/
ProgressMgr::~ProgressMgr()
{
}

/**
**/
Bio::ProgressItem* ProgressMgr::startProgress( const QString &msg, int totalSteps, bool canCancel )
{
	return new Progress( msg, totalSteps, canCancel, m_progressDlg );
}

/**
**/
Progress::Progress( const QString &msg, int totalSteps, bool canCancel, ProgressDlg *dlg ) : m_progressId( -1 ),
	m_totalSteps( totalSteps ), m_curProgress( 0 ), m_buffer( 0 ), m_minUpdate( totalSteps / 200 ),
	m_dlg( dlg )
{
	if( m_minUpdate < 1 )
		m_minUpdate = 1;

	if( dlg )
		m_progressId = dlg->start( msg, totalSteps, canCancel );
}

/**
**/
Progress::~Progress()
{
	if( m_progressId >= 0 )
		finish();
}

/**
**/
bool Progress::isCanceled() const
{
	if( !m_dlg )
		return false;

	return m_dlg->isCanceled();
}

/**
**/
void Progress::step( int steps )
{
	if( !m_dlg || m_progressId < 0 )
		return;

	m_dlg->stepProgress( m_progressId, steps );
}

/**
**/
void Progress::update( int curSteps )
{
	if( !m_dlg || m_progressId < 0 )
		return;

	m_dlg->setProgress( m_progressId, curSteps );
}

/**
**/
void Progress::finish()
{
	if( !m_dlg || m_progressId < 0 )
		return;

	m_dlg->finish( m_progressId );
	m_progressId = -1;
}

/**
**/
void Progress::cancel()
{
	if( m_dlg )
		m_dlg->cancel();
}

/**
**/
void Progress::status( const QString &msg )
{
	if( m_dlg )
		m_dlg->setInfo( m_progressId, msg );
}
